package treeStudy;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class DelNodes {

    private List<TreeNode> ans = new ArrayList<>();

    /**
     * LeetCode T1110 删点成林
     * @param args
     */
    public static void main(String[] args) {

    }

    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < to_delete.length; i++){
            set.add(to_delete[i]);
        }
        TreeNode t = dfs(root, set);
        if (t != null){
            ans.add(t);
        }
        return ans;
    }

    public TreeNode dfs(TreeNode root, HashSet<Integer> to_delete){
        if (root == null){
            return null;
        }
        TreeNode left = dfs(root.left, to_delete);
        TreeNode right = dfs(root.right, to_delete);

        if (to_delete.contains(root.val)){
            if (left != null){
                ans.add(left);
            }
            if (right != null) {
                ans.add(right);
            }
            return null;
        } else {
            root.left = left;
            root.right = right;
            return root;
        }
    }
}
